package com.zhu.shiroweb.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhu.shiroweb.entity.User;



/**
 * 
 * @author zhu
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	/**
	 *
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/login")
	public String login(User user,HttpServletRequest request){
		//获取当前登录用户
		Subject subject=SecurityUtils.getSubject();
		//封装表单中提交的用户名和密码
		UsernamePasswordToken token=new UsernamePasswordToken(user.getUserName(), user.getPassword());
		try{
			//调用login方法，传入封装好的token
			subject.login(token);
			//登录成功跳转success.jsp
			return "redirect:/success.jsp";
		}catch(Exception e){
			e.printStackTrace();
			//登录失败就重新登录
			request.setAttribute("errorMsg", "登录失败");
			return "login";
		}
	}
	

}
